package com.xing.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xing.servicebase.exceptionhandler.GuliException;
import com.xing.serviceedu.entity.EduChapter;
import com.xing.serviceedu.entity.EduVideo;
import com.xing.serviceedu.entity.chapter.ChapterVo;
import com.xing.serviceedu.entity.chapter.VideoVo;
import com.xing.serviceedu.entity.vo.CourseWebVo;
import com.xing.serviceedu.mapper.EduChapterMapper;
import com.xing.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xingfuao
 * @since 2022-01-09
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    //1.删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId章节ID查询小节表，如果查询数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        //根据chapterId查询出多少记录
        int count = videoService.count(wrapper);
        //判断,章节下有小节，就不删除，否则删除
        if(count>0){
            throw new GuliException(20001,"不能删除");
        }else { //不能查询数据，进行删除
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    //2.根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }

    /**
     * 3.得到章节视图VO通过课程id
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVoByCourseId(String courseId) {
        //1.根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterQueryWrapper);
        //2.根据课程id查询课程里面所有小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(videoQueryWrapper);
        //创还能list集合进行最终封装
        ArrayList<ChapterVo> finalList = new ArrayList<>();
        //3.遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);

            ArrayList<VideoVo> finalVideoVoList = new ArrayList<>();
            //4.遍历查询小节list集合进行封装
            for (int j = 0; j < eduVideoList.size(); j++) {
                EduVideo eduVideo = eduVideoList.get(j);
                if(eduVideo.getChapterId().equals(chapterVo.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    finalVideoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(finalVideoVoList);
        }
        return finalList;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
       return baseMapper.getBaseCourseInfo(courseId);
    }
}
